package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.log.DouLuoDaLuEventNoticeData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DouLuoDaLuEventNoticeDataDaoImpl extends TempDataObjectDao<DouLuoDaLuEventNoticeData> {



    public static String getDouLuoDaLuEventNoticeDataSql = "select * from `t_c_douluodalu_event_notice` where `activityId`=? and  `logTime` >? and groupId=? order by `logTime` desc limit 100";
    public List<DouLuoDaLuEventNoticeData> getDouLuoDaLuEventNoticeData(int activityId, long todayTime, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<DouLuoDaLuEventNoticeData> result = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuEventNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, todayTime);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                result.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertDouLuoDaLuEventNoticeDataSql = "insert into `t_c_douluodalu_event_notice`(activityId,userId,type,regionId,rewards,consume,monsterId,eventId,paramList,userBaseInfo,enemyBaseInfo," +
            "attackType,friendBaseInfo,logTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuEventNoticeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRegionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRewards()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getConsume()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getParamList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyBaseInfo() == null ? "" : JSON.toJSONString(data.getEnemyBaseInfo())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAttackType()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getFriendBaseInfo() == null ? "" : JSON.toJSONString(data.getFriendBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLogTime()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuEventNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuEventNoticeDataSql = "update `t_c_douluodalu_event_notice` set userId=?,type=?,regionId=?,rewards=?,consume=?,monsterId=?,eventId=?,paramList=?,userBaseInfo=?,enemyBaseInfo=?,logTime=? where activityId=?";

    @Override
    protected boolean updateData(DouLuoDaLuEventNoticeData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRegionId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRewards()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getConsume()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getParamList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getUserBaseInfo())));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyBaseInfo() == null ? "" : JSON.toJSONString(data.getEnemyBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLogTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuEventNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuEventNoticeData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuEventNoticeData noticeData = new DouLuoDaLuEventNoticeData();
        noticeData.setActivityId(rs.getInt("activityId"));
        noticeData.setUserId(rs.getLong("userId"));
        noticeData.setType(rs.getInt("type"));
        noticeData.setRegionId(rs.getInt("regionId"));
        noticeData.setRewards(rs.getString("rewards"));
        noticeData.setConsume(rs.getString("consume"));
        noticeData.setMonsterId(rs.getInt("monsterId"));
        noticeData.setEventId(rs.getInt("eventId"));
        noticeData.setParamList(JSON.parseObject(rs.getString("paramList"), new TypeReference<List<String>>() {}));
        noticeData.setUserBaseInfo(JSON.parseObject(rs.getString("userBaseInfo"), UserBaseInfo.class));
        String str = rs.getString("enemyBaseInfo");
        if(!StringUtils.isNullOrEmpty(str) && !"{}".equals(str)){
            noticeData.setEnemyBaseInfo(JSON.parseObject(str, UserBaseInfo.class));
        }
        str = rs.getString("friendBaseInfo");
        if(!StringUtils.isNullOrEmpty(str) && !"{}".equals(str)){
            noticeData.setFriendBaseInfo(JSON.parseObject(str, UserBaseInfo.class));
        }
        noticeData.setAttackType(rs.getInt("attackType"));
        noticeData.setLogTime(rs.getLong("logTime"));
        return noticeData;
    }
}
